package com.zat.mes.andrmes.dofun.system.vm

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.MutableLiveData
import com.google.gson.Gson
import com.love.zywanandroid.retrofit.RetrofitManager
import com.love.zywanandroid.retrofit.api.ApiService
import com.zat.mes.andrmes.R
import com.zat.mes.andrmes.data.BaseResponse
import com.zat.mes.andrmes.data.ClockBean
import com.zat.mes.andrmes.retrofit.callback.MesCallBack
import okhttp3.RequestBody
import retrofit2.Call
import retrofit2.Response

/**
 * @author zy
 * @date :2020-02-17 16:53
 * @description: 审核 取消审核model
 */
class ClockReadViewModel(application: Application) : AndroidViewModel(application) {

    private val clockBeanData: MutableLiveData<List<ClockBean>> = MutableLiveData()
    private val clockReadData: MutableLiveData<String> = MutableLiveData()

    fun getClockReadList(): MutableLiveData<List<ClockBean>> {
        return clockBeanData
    }

    fun getClockReadData(): MutableLiveData<String> {
        return clockReadData
    }


    /**
     * 获取列表
     */
    fun loadClockReadList(day: String, empId: Long, status: String) {
        RetrofitManager.apiService(ApiService::class.java)
            .getClockRead(day, empId, status)
            .enqueue(object : MesCallBack<BaseResponse<List<ClockBean>>>() {
                override fun onSuccessful(
                    call: Call<BaseResponse<List<ClockBean>>>?,
                    response: Response<BaseResponse<List<ClockBean>>>?
                ) {
                    clockBeanData.postValue(response?.body()?.result)
                }

            })
    }

    /**
     * 审核 取消审核
     */
    fun loadClockRead(list: List<ClockBean>, read: Boolean) {
        val toJson = Gson().toJson(list);
        val requestBody: RequestBody = RequestBody.create(RetrofitManager.JOSNTYPE, toJson)
        if (read) {
            //审核
            RetrofitManager.apiService(ApiService::class.java)
                .clockRead(requestBody)
                .enqueue(object : MesCallBack<BaseResponse<String>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<String>>?,
                        response: Response<BaseResponse<String>>?
                    ) {
                        if (response?.body()?.success != null) {
                            if (response?.body()?.success!!) {
                                clockReadData.postValue(response?.body()?.message)
                            } else {
                                clockReadData.postValue(response?.body()?.result)
                            }
                        } else {
                            clockReadData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                        }
                    }

                })
        } else {
            //取消审核
            RetrofitManager.apiService(ApiService::class.java)
                .clockUnRead(requestBody)
                .enqueue(object : MesCallBack<BaseResponse<String>>() {
                    override fun onSuccessful(
                        call: Call<BaseResponse<String>>?,
                        response: Response<BaseResponse<String>>?
                    ) {
                        if (response?.body()?.success != null) {
                            if (response?.body()?.success!!) {
                                clockReadData.postValue(response?.body()?.message)
                            } else {
                                clockReadData.postValue(response?.body()?.result)
                            }
                        } else {
                            clockReadData.postValue(getApplication<Application>().getString(R.string.net_request_fail))
                        }
                    }

                })
        }
    }
}